<?php
class primative
{
    function getUrls($string)
    {
        $regex = '/https?\:\/\/[^\" ]+/i';
        preg_match_all($regex, $string, $matches);
        //return (array_reverse($matches[0]));
        return ($matches[0]);
    }

    //xpath html
    public static function xpath($dom_xpath, $xpath_query, $strip = true)
    {
        //Put your XPath Query here
        $result_rows = $dom_xpath->query($xpath_query);

        //here we loop through our results (a DOMDocument Object)
        foreach ($result_rows as $result_object)
        {
            if($strip)
            {
                $newdoc = new DOMDocument;
                $node = $newdoc->importNode($result_object, true);
                $newdoc->appendChild($node);
                $html = $newdoc->saveHTML();
                $result = strip_tags($html);
                return $result;
            }
            else
            {
                return $result_object->textContent;
            }
        }
        return null;
    }
    public static function xpath_images($dom_xpath, $xpath_query)
    {
        //Put your XPath Query here
        $result_rows = $dom_xpath->query($xpath_query);
        $result = null;
        $index = 0;

        //here we loop through our results (a DOMDocument Object)
        foreach ($result_rows as $result_object)
        {
            $newdoc = new DOMDocument;
            $node = $newdoc->importNode($result_object, true);
            $newdoc->appendChild($node);
            $html = $newdoc->saveHTML();
            $result = primative::getUrls($html);

            //$result[$index] = $result_object->textContent;
            //$index++;
        }
        return $result;
    }

    public static function xpath_asis($dom_xpath, $xpath_query)
    {
        //Put your XPath Query here
        $result_rows = $dom_xpath->query($xpath_query);
        $result = null;
        $index = 0;

        //here we loop through our results (a DOMDocument Object)
        foreach ($result_rows as $result_object)
        {
            $newdoc = new DOMDocument;
            $node = $newdoc->importNode($result_object, true);
            $newdoc->appendChild($node);
            $html = $newdoc->saveHTML();
            $result = $html;

            //$result[$index] = $result_object->textContent;
            //$index++;
        }
        return $result;
    }

    public static function do_get_primative_data($post)
    {
        $folder_url = '';
        include('include/config.php');
        if($post['title'] == null)
            return null;

        $sourceurl = $post['sourceurl'];
        $html = util::read_url($sourceurl);
        $dom = new DOMDocument();
        @$dom->loadHTML($html);
        $dom_xpath = new DOMXPath($dom);

        $title = $post['title'];
        $type = $post['type'];
        $multi = $post['multi'];
        $xpath = $post['xpath'];
        $attribute = $post['attribute'];
        $asis = $post['asis'];
        $place_holder = $post['place_holder'];

        $output = "";
        include('include/config.php');

        foreach($title as $a => $b)
        {
            //$data['output'] will have these information
            /*
            a. Body
            b. Images
            d. Below Poster
            */

            $xpath[$a] = stripslashes($xpath[$a]);

            if($title[$a] != "")
            {
                $output.="<h2 zone='{30}'>{0}</h2>";
                $output = str_replace("{0}", $title[$a], $output);
            }


            if($attribute[$a] != "")
            {
                $xpath[$a] .= '/@'.$attribute[$a];
            }

            if($type[$a] == 'img')
            {
                if($multi[$a] == '1' )
                {
                    $images = primative::xpath_images($dom_xpath, $xpath[$a]);
                    if($images != null)
                    {
                        foreach($images as $src)
                        {
                            if(strpos($src, '.jpg') !== false)
                            {
                                $output .= '<img zone="{30}" src="{0}" />';
                                $src = util::add_domain_to_link_by_url($sourceurl, $src);
                                $src = $folder_url.'dl/'.base64_encode($src);
                                $output = str_replace("{0}", $src, $output);
                            }
                        }
                    }
                }
                else
                {
                    $src = primative::xpath($dom_xpath, $xpath[$a], false);
                    if($src != null)
                    {
                        $output .= '<img zone="{30}" src="{0}" />';
                        $src = util::add_domain_to_link_by_url($sourceurl, $src);
                        $src = $folder_url.'dl/'.base64_encode($src);
                        $output = str_replace("{0}", $src, $output);
                    }
                }
            }
            else //p, div, h1, etc...
            {
                if($asis[$a] == '1')
                {
                    $output .= "<$type[$a] zone='{30}'>{0}</$type[$a]>";
                    $output = str_replace("{0}", primative::xpath_asis($dom_xpath, $xpath[$a]), $output);
                }
                else if($multi[$a] == '1' )
                {
                    //to be done...
                }
                else
                {
                    $xpath[$a] .= '/text()';
                    $output .= "<$type[$a] zone='{30}'>{0}</$type[$a]>";
                    $output = str_replace("{0}", primative::xpath($dom_xpath, $xpath[$a]), $output);
                }
            }
            $output = str_replace("{30}", $place_holder[$a], $output);
            $data['output'] = $output;





            if($place_holder[$a] == 'c')//poster
            {
                $src = primative::xpath($dom_xpath, $xpath[$a], false);
                if($src != null)
                    $src = $folder_url.'dl/'.base64_encode($src);
                else
                    $src = 'no-poster.jpg';
                $data['poster'] = $src;
            }
            else if($place_holder[$a] == 'j')//description
            {
                $description = primative::xpath($dom_xpath, $xpath[$a]);
                $data['description'] = $description;
            }


            //values to be collected and saved in below variables...
            /*
            c. Poster
            e. Rating
            f. Votes
            g. Release Date
            h. Category
            i. Genre
            j. Description
            k. Similar

            */

            $data['rating'] = '';
            $data['votes'] = '';
            $data['release_date'] = '';
            $data['category'] = '';
            $data['genre'] = '';
            $data['similar'] = '';
        }

        return $data;
    }

    public static function get_primative_data($sitemap_meta_id, $udnurl, $sourceurl, $content_type)
    {
        //echo "fetching ".$sitemap_meta_id.", ".$udnurl.", ".$sourceurl.", ".$content_type;
        //check if script data is cached already...
        if($udnurl == null)
            return null;

        $data['output'] = services::get_script_cache($content_type, $udnurl, 'primative');

        if($data['output'] == null)// if not cached then cache it
        {
            $post = services::get_content_xpath_as_post($sitemap_meta_id);
            $post['sourceurl'] = $sourceurl;
            $data = primative::do_get_primative_data($post);

            if($data != null)
            {
                services::insert_script_cache($content_type, $udnurl, 'primative', $data['output']);

                //insert these values in sitemap_data_$content_type
                $data['poster'];
                $data['rating'];
                $data['votes'];
                $data['release_date'];
                $data['category'];
                $data['description'];
                $data['similar'];
            }
        }
        return $data['output'];
    }


}
